﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web;

namespace Al.UI.Controls
{
    public class CometRequestor: Control, IScriptControl
    {
        #region Public Properties

        [DefaultValue(1000)]
        public int PollTime
        {
            get
            {
                object result = this.ViewState["PollTime"] ?? true;
                return (int)result;
            }
            set
            {
                this.ViewState["PollTime"] = value;
            }
        }

        public string SessionID
        {
            get
            {
                object result = this.ViewState["SessionID"] ?? true;
                return result.ToString();
            }
            set
            {
                this.ViewState["SessionID"] = value;
            }
        }

        #endregion

        #region Base Class Overrides

        protected override void OnPreRender(EventArgs e)
        {
            ScriptManager scriptManager = ScriptManager.GetCurrent(Page);

            if (scriptManager == null)
                throw new InvalidOperationException("ScriptManager required on the page.");

            scriptManager.RegisterScriptControl(this);

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(Page);

                if (scriptManager == null)
                    throw new HttpException("A ScriptManager control must exist on the current page.");
                scriptManager.RegisterScriptDescriptors(this);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();

            base.Render(writer);
        }

        #endregion

        #region IScriptControl Members

        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Al.UI.Controls.Repeater", this.ClientID);
            descriptor.AddProperty("pollTime", this.PollTime);
            descriptor.AddProperty("sessionID", this.SessionID);

            return new ScriptDescriptor[] { descriptor };
        }

        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            ScriptReference reference = new ScriptReference();
            if (Page != null)
                reference.Path = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Al.UI.Controls.Comet.CometRequestor.js");

            return new ScriptReference[] { reference };
        }

        #endregion
    }
}
